
var loading = false; //状态标记
var app = new Vue({
    el: '#app',
    data: {
        onLoading:true,//页面加载中
        id: 1, //话题ID
        pageIndex: 1, //当前第一页
        pageSize: 10, //每页10条
        total: '',
        listTab: [],
        sendContent: '', //评论内容
        listData: { total: 0, list: [] },
        userId: 0, //用户id
        hideing: true, //默认为正在加载 false隐藏
        moreName: ['正在加载'],
        pageShow: true, //显示页面
        objectId: '',
        commentList: { list: [], total: 0 },
    },
    created() {
        FastClick.attach(document.body);
        if ($.getPara("userId") != "") {
            var userId = $.getPara("userId");
            this.userId = userId;
            //缓存用户ID
            $.setStorage("userId", userId);
        } else {
            if ($.getStorage("userId") && $.getStorage("userId") != "") {
                var userId = parseInt($.getStorage("userId"));
                this.userId = userId;
            }
        }
        var id = $.GetRequest().id //获取上一级页面的id参数
        if (id || this.id) { //获取话题的id
            this.objectId = id;
            this.getData();
            this.getCommentList();
        } else {
            $.toast('获取话题ID失败', 'text')
            this.pageShow = false;
            // console.log("没有当前页面话题详情id")
        }
    },
    mounted() {
        var that = this;
        this.$nextTick(function () {
            $(document.body).infinite().on("infinite", function () {
                if (loading) return;
                loading = true;
                that.hideing = false;
                setTimeout(function () {
                    var pageIndex = that.pageIndex;
                    pageIndex++;
                    that.pageIndex = pageIndex;
                    that.getCommentList();
                    loading = false;
                }, 1500); //模拟延迟
            });
            // 此处填第一次渲染完成后执行的代码
        })
    },
    methods: {
        //话题详情
        getData() {
            var that = this;
            $.ajax({
                url:  "api/topic/detail",
                type: 'get',
                dataType: 'json',
                data: {
                    id: 15, //：话题ID
                },
            }).done(function (res, status, xhr) {
                if (res.code == 200) {
                    var list = res.data;
                    that.listTab = list;
                    that.onLoading = false;
                } else {
                    //其他请求状态提示
                    $.toast(res.msg, "text");
                }
                // console.log(data)

            });
        },
        //评论
        getCommentList() {
            var that = this;
            var objectId = that.objectId;
            $.ajax({
                type: "GET",
                url: "/api/comment/common/list",
                dataType: 'json',
                data: {
                    type: 2,  //评论对象类型：1故事回忆，2话题，3学院，4协会，5媒体，6心情，7动态，8评测 9活动
                    objectId: objectId,
                    userId: that.userId,
                    pageIndex: that.pageIndex,
                    pageSize: that.pageSize
                },
                success: function (res) {
                    if (res.code == 200) {
                        var list = res.data.list;
                        that.commentList.total = res.data.total;
                        if (that.commentList.list.length > 0) {//原来有数据，加载更多
                            if (list.length > 0) {
                                var original = that.commentList.list;
                                var array = original.concat(list);
                                that.commentList.list = array;
                            } else {
                                loading = true;
                                $.toast("没有更多了...", "text")
                            }
                            //更新状态
                            that.hideing = true;
                        } else {//
                            that.commentList.list = res.data.list;
                            that.hideing = true;
                            loading = true;
                        }

                    } else {
                        //添加数据
                        $.toast(res.msg, "text");
                    }
                },
                error: function (xhr, textStatus) {
                },
            })
        },
        //提交发布  
        postSubmit() {
            let _this = this;
            if(_this.userId==0){
                $.toast("请登陆！", "text");
                return false;
            }
            str=_this.sendContent
            str = str.replace(/\n/g, '');
            if ($.javaTrim(str) == "") {
                $.toast("输入不能全为空",'text');
                return false;
            }
            if (_this.sendContent === '') {
                $.toast("请输入评论", "text");
                return false;
            }
            $.ajax({
                url:  "api/comment/common/publish",
                type: 'post',
                dataType: 'json',
                data: {
                    type: 2,
                    objectId: _this.objectId,
                    userId: _this.userId,
                    comment: _this.sendContent,
                },
            }).done(function (data, status, xhr) {
                if (data.code == 200) {
                    $.toast('提交成功', 'text')
                    _this.sendContent = '';
                    _this.pageIndex = 1;//更新当前一页
                    _this.commentList.list = [];//清除当前的列表数据
                    _this.getCommentList();
                } else {
                    $.toast('提交失败', 'text')
                }
            });
        }

    }
});